package com.xinsoft.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.*;
import com.xinsoft.entity.po.MesProducePlan;
import com.xinsoft.entity.po.ScmPushBill;
import com.xinsoft.entity.po.SysConfig;
import com.xinsoft.entity.po.SysMaterial;
import com.xinsoft.entity.vo.*;
import com.xinsoft.service.MesProducePlanService;
import com.xinsoft.service.ScmPushBillService;
import com.xinsoft.service.SyncPullBillService;
import com.xinsoft.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SyncPullBillAdapter{

    private SyncPullBillService syncPullBillService;

    @Autowired
    private MesProducePlanService mesProducePlanService;

    @Autowired
    private SyncPullBillKisServiceImpl syncPullBillKisServiceImpl;

    @Autowired
    private SysConfigService configService;
    @Autowired
    private ScmPushBillService pushBillService;

    private String ERP_CONNECT_TYPE;

    @PostConstruct
    private void init() {
        //读取配置文件，然后给 syncTemplateService 赋初始值
        List<SysConfig> sysConfigDOS = configService.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamKey, Const.ERP_CONNECT_TYPE).or().eq(SysConfig::getParamKey, Const.ERP_CONNECT_TYPE));
        sysConfigDOS.forEach(item ->{
            if ("ERP_CONNECT_TYPE".equals(item.getParamKey()))
                ERP_CONNECT_TYPE = item.getParamValue();
        });
    }

    public Response<?> productionSync() {
        List<MesProducePlan> planList = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) planList = syncPullBillKisServiceImpl.productionSync();
        mesProducePlanService.insertOrUpdateBatch(planList);
        return Response.succeed("操作成功");
    }

    public Response<?> productionSyncTwoYears() {
        Date nextMonth = DateUtil.offsetMonth(new Date(),2);
        DateTime startTime = DateUtil.parse("2021-10-01", "yyyy-MM-dd");
        DateTime endTime = new DateTime();
        for (endTime = DateUtil.offsetMonth(startTime,2) ; startTime.before(nextMonth); ){

            List<MesProducePlan> planList = new ArrayList<>();
            planList = syncPullBillKisServiceImpl.productionSyncTwoYears(startTime, endTime);
            if (ObjectUtil.isNotEmpty(planList)){
                mesProducePlanService.insertOrUpdateBatch(planList);
            }

            startTime = DateUtil.offsetMonth(startTime,2);
            endTime = DateUtil.offsetMonth(endTime,2);
        }
        return Response.succeed("操作成功");
    }

    /**
     * 生产投料单分页
     */
    public Response<?> feedingSync(MesFeedingPlanPageParam param) {
        List<ErpProductionFeedingVO> voIPage = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) voIPage = syncPullBillKisServiceImpl.feedingSync(param);
        return Response.succeed(voIPage);
    }

    /**
     * 生产投料单详情
     */
    public Response<?> feedingDetailSync(List<Integer> ids) {
        List<ErpProductionFeedingDetailVO> detailVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) detailVOS = syncPullBillKisServiceImpl.feedingDetailSync(ids);
        return Response.succeed(detailVOS);
    }

    /**
     * 领料出库单分页
     */
    public Response<?> feedingOutSync(ErpFeedingOutPageParam param) {
        List<ErpFeedingOutVO> voIPage = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) voIPage = syncPullBillKisServiceImpl.feedingOutSync(param);
        return Response.succeed(voIPage);
    }

    /**
     * 领料出库单分页详情
     */
    public Response<?> feedingOutDetailSync(List<Integer> ids) {
        List<ErpFeedingOutDetailVO> detailVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) detailVOS = syncPullBillKisServiceImpl.feedingOutDetailSync(ids);
        return Response.succeed(detailVOS);
    }

    /**
     * 委外订单
     * @return
     */
    public List<ErpOutSourcingVO> sourcing(ErpSourcingPageParam param) {
        List<ErpOutSourcingVO> voIPage = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) voIPage = syncPullBillKisServiceImpl.sourcing(param);
        return voIPage;
    }

    public Response<?> sourcingItem(List<Integer> ids) {
        List<ErpOutSourcingItemVO> itemVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) itemVOS = syncPullBillKisServiceImpl.sourcingItem(ids);
        return Response.succeed(itemVOS);
    }

    public Response<?> sourcingItem(ErpSourcingPageParam param) {
        List<ErpOutSourcingItemVO> itemVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) itemVOS = syncPullBillKisServiceImpl.sourcingItem(param);
        return Response.succeed(itemVOS);
    }

    /**
     * 委外投料
     * @param param
     * @return
     */
    public Response<?> sourcingFeeding(ErpSourcingFeedingPageParam param) {
        List<ErpSourcingFeedingVO> itemVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) itemVOS = syncPullBillKisServiceImpl.sourcingFeeding(param);
        return Response.succeed(itemVOS);
    }

    public Response<?> sourcingFeedingDetail(List<Integer> ids) {
        List<ErpSourcingFeedingItemVO> itemVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) itemVOS = syncPullBillKisServiceImpl.sourcingFeedingDetail(ids);
        return Response.succeed(itemVOS);
    }

    /**
     * 采购订单
     * @param param
     * @return
     */
    public Response<?> purchase(ErpPurchasePageParam param) {
        List<ErpPurchaseVO> list = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) list = syncPullBillKisServiceImpl.purchase(param);
        return Response.succeed(list);
    }

    public Response<?> purchaseDetail(List<Integer> ids) {
        List<ErpPurchaseDetailVO> list = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) list = syncPullBillKisServiceImpl.purchaseDetail(ids);
        return Response.succeed(list);
    }

    /**
     * 采购收料通知
     * @return
     */
    public Response<?> purchaseNoticeSync(ErpPurchaseNoticePageParam param) {
        List<ErpPurchaseNoticeVO> voIPage = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) voIPage = syncPullBillKisServiceImpl.purchaseNoticeSync(param);
        return Response.succeed(voIPage);
    }

    public Response<?> purchaseNoticeDetailSync(List<Integer> ids) {
        List<ErpPurchaseNoticeItemVO> detailVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) detailVOS = syncPullBillKisServiceImpl.purchaseNoticeDetailSync(ids);
        return Response.succeed(detailVOS);
    }

    public Response<?> purchaseNoticeDetailSync(ErpPurchaseNoticePageParam param) {
        List<ErpPurchaseNoticeItemVO> detailVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) detailVOS = syncPullBillKisServiceImpl.purchaseNoticeDetailSync(param);
        return Response.succeed(detailVOS);
    }

    public Response<?> saleNoticeSync(ScmSaleNoticeParam param) {
        IPage<ScmSaleNoticeListVo> voIPage = new Page<>();
        if (ERP_CONNECT_TYPE.equals("1")) voIPage = syncPullBillKisServiceImpl.saleNoticeSync(param);
        return Response.succeed(voIPage);
    }

    public Response<?> saleNoticeDetailSync(Integer id) {
        ScmSaleNoticeVo detailVO = syncPullBillService.saleNoticeDetailSync(id);
        return Response.succeed(detailVO);
    }

    public Response<?> saleSync(ErpSalePageParam param) {
        List<ErpSaleVO> voIPage = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) voIPage = syncPullBillKisServiceImpl.saleSync(param);
        return Response.succeed(voIPage);
    }

    public Response<?> saleDetailSync(List<Integer> ids) {
        List<ErpSaleDetailVO> detailVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) detailVOS = syncPullBillKisServiceImpl.saleDetailSync(ids);
        ErpRealTimePageParam param = new ErpRealTimePageParam();
        param.setCurrentPage(1);
        param.setPageSize(100);
        Map<Integer, ScmPushBill> waitPushBillMap = pushBillService.getWaitPushBillMap(Const.BillType.XSCK, ids);
        detailVOS.forEach(vo -> {
            if (Objects.nonNull(waitPushBillMap)){
                vo.setExistNotSubmit(waitPushBillMap.containsKey(vo.getMaterialId()));
            }
            param.setMaterialId(vo.getMaterialId().toString());
            List<ErpRealTimeDto> records = this.realtimeInventory(param).getRecords();
            List<ErpRealTimeDto> realTimeDtoList = records.stream()
                    // .filter(dto -> !Objects.equals(dto.getWarehouseItemId(), 0) || !Objects.equals(dto.getWarehouseItemName(), "*"))
                    .limit(3)
                    .collect(Collectors.toList());
            vo.setRealtimeInventoryList(realTimeDtoList);
        });
        return Response.succeed(detailVOS);
    }

    public Response<?> stocktakingList(ErpSalePageParam param) {
        List<ErpStocktakingVO> listVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) listVOS = syncPullBillKisServiceImpl.stocktakingList(param);
        return Response.succeed(listVOS);
    }

    public Response<?> stocktakingDetail(Integer id) {
        List<ErpStocktakingDetailVO> detailVOS = new ArrayList<>();
        if (ERP_CONNECT_TYPE.equals("1")) detailVOS = syncPullBillKisServiceImpl.stocktakingDetail(id);
        return Response.succeed(detailVOS);
    }

    /**
     * 实时库存
     * @return
     */
    public IPage<ErpRealTimeDto> realtimeInventory(ErpRealTimePageParam param) {
        IPage<ErpRealTimeDto> voIPage = new Page<>();
        if (ERP_CONNECT_TYPE.equals("1")) voIPage = syncPullBillKisServiceImpl.realtimeInventory(param);
        return voIPage;
    }


}
